﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutoMapper;
using TheNorthStar.Web.Application;

using TheNorthStar.Web.Application.Services;
using TheNorthStar.Api.Requests;
using TheNorthStar.Api.Results;
using TheNorthStar.Domain;

namespace TheNorthStar.Web.Controllers
{
    public class DriversController : Controller
    {
        private readonly IDriverLoginService _driverLoginService;

        public DriversController(IDriverLoginService driverLoginService)
        {
            _driverLoginService = driverLoginService;
        }
        [HttpGet]
     
        public JsonResult Index(string id)
        {
           /* if (id != null)
            {
                DriverLogin logDriver = _driverLoginService.Get( id );
               // string jsonResult = Newtonsoft.Json.JsonConvert.SerializeObject(logDriver);
                if (logDriver.userName != null && logDriver.passWord != null)
                {
                    return Json( 1, JsonRequestBehavior.AllowGet);
                }
                else
                {

                    return Json("No Fucking user or Wrong fucking password!", JsonRequestBehavior.AllowGet);
                }
            }*/
            return Json(new { success = false,message ="hi" }, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public JsonResult Index(int id)
        {
            return Json(new { success = false }, JsonRequestBehavior.AllowGet);
        }
       
        [HttpPost]
        public JsonResult Logon(DriverLogon  driverDetails)
        {

            DriverLogonResult result = new DriverLogonResult();
            Driver driver = _driverLoginService.GetByUsernameAndPassword(driverDetails.userName, driverDetails.password);
            if (driver == null)
            {
                result.logonSuccess = false;
                result.msg = "wrong!!";
            }
            else
            {
                result.logonSuccess = true;
                result.numOfcarSeats = driver.Car.MaxPassengers.Value;
                result.carType = driver.Car.Type;
                result.driverId = driver.DriverId;
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public JsonResult SetDriverStatus(DriverLogon myDriver)
        {
            Driver driver = _driverLoginService.GetDriverByID( myDriver.driverID );
            DriverLogonResult result = new DriverLogonResult();
            if (driver != null)
            {
                int id = driver.DriverId;
                _driverLoginService.SetDriverStatus(id, myDriver.driverStatus);
                result.msg = driver.DriverStatus;
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public JsonResult AcceptBooking(DriverLogon driver, TheNorthStar.Api.Results.Booking booking)
        {
            
            var result = _driverLoginService.AcceptBooking(driver.driverID, booking.BookingId);
            return Json(new {success = result});
        }
        [HttpPost]
        public JsonResult PickedUpBooking(DriverLogon driver, TheNorthStar.Api.Results.Booking booking)
        {

            var result = _driverLoginService.PickedUpBooking(driver.driverID, booking.BookingId);
            
            return Json(new { success = result });
        }
        [HttpPost]
        public JsonResult NoShow(DriverLogon driver, TheNorthStar.Api.Results.Booking booking)
        {

            var result = _driverLoginService.NoShow(driver.driverID, booking.BookingId);

            return Json(new { success = result });
        }
        [HttpPost]
        public JsonResult CompleteJob(DriverLogon driver, TheNorthStar.Api.Results.Booking booking)
        {
            var result = _driverLoginService.CompleteJob( driver.driverID, booking.BookingId );
            return Json(new { success = result });
        }
        [HttpPost]
        public JsonResult RequestWork(DriverMoreWorkRequest request)
        {
            Driver driver = _driverLoginService.GetDriverByID( request.driverID );

            if(driver != null)
            {
                var booking = _driverLoginService.RequestWork(driver.DriverId, request.Lat, request.Lng);

                var res = Mapper.Map<Domain.Booking, Api.Results.Booking>(booking);
                
                

                return Json(res);
            }
            return Json(new { success = false }, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public JsonResult GetCustomerAddress( TheNorthStar.Api.Results.Booking booking )
        {

            return Json(_driverLoginService.GetCustomersAdd(booking.BookingId), JsonRequestBehavior.AllowGet);
        }

        public void UpdatePosition(DriverPosition pos)
        {
            
            _driverLoginService.UpdatePosition(pos);

            
        }
        /* 

        
         //
         // GET: /Bookings/
         [HttpGet]
         public JsonResult Index(int? id)
         {
             if(id.HasValue)
             {
                 Booking booking = _bookingService.Get(id.Value);
                 string jsonResult = Newtonsoft.Json.JsonConvert.SerializeObject(booking);
                 return Json(jsonResult, JsonRequestBehavior.AllowGet);
             }

             //403 = forbidden, at this stage only admins can get the list of overall bookings
             Response.StatusCode = 403;
             return Json(new { success = false }, JsonRequestBehavior.AllowGet);
         }

         [HttpPost]
         public JsonResult Index(int id)
         {
             return Json(new { success = false }, JsonRequestBehavior.AllowGet);
         }
 */

    }
}
